<template>
  <div class="page_post app-container" v-loading="loading">
    <cfCard title='' class='page-container-search bottom12'>
      <div slot='content'>
        <el-form :model="searchParams" ref="searchParams" size="small" :inline="true" label-width="68px">
          <el-form-item label="岗位编码" prop="postCode">
            <el-input v-model="searchParams.postCode" placeholder="请输入岗位编码" clearable v-search="handleSearch"/>
          </el-form-item>
          <el-form-item label="岗位名称" prop="postName">
            <el-input v-model="searchParams.postName" placeholder="请输入岗位名称" clearable v-search="handleSearch"/>
          </el-form-item>
          <el-form-item label="状 态" prop="status">
            <el-select v-model="searchParams.status" placeholder="岗位状态" clearable @change="handleSearch">
              <el-option v-for="dict in sysNormalDisable" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </cfCard>
    <cfCard title='岗位列表' hasTable :tableObj='tableDataObj' class='page_house-container-bottom'>
      <!--表格区域-->
      <template slot='btn'>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:post:add']">新增</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:post:export']">导出</el-button>
      </template>
      <template slot='content'>
        <cfTable ref="cfTable" :tableDataObj='tableDataObj' :tableData='tableData' @handleSelectionChange="handleSelectionChange">
          <template slot="status">
            <el-table-column label='状 态' width='100px' align='center'>
              <dict-tag slot-scope="scope" :options="sysNormalDisable" :value="scope.row.status"/>
            </el-table-column>
          </template>
          <template slot="createTime">
            <el-table-column label='创建时间' width='180px'>
              <span slot-scope="scope">{{ parseTime(scope.row.createTime) }}</span>
            </el-table-column>
          </template>
          <template slot='operate'>
            <el-table-column label='操 作' width='100px' align='right'>
              <span slot-scope='{row}'>
                <el-button type='text' size='small' @click='handleEdit(row)'>编辑</el-button>
                <el-button type='text' size='small' @click='handleDelete(row)'>删除</el-button>
              </span>
            </el-table-column>
          </template>
        </cfTable>
        <pagination :total='queryParams.total' :page.sync='queryParams.pageNum' :limit.sync='queryParams.pageSize' @pagination='getList'/>
      </template>
    </cfCard>
    <updateDialog v-if="dialogObj.visible" :dialogObj="dialogObj" @refresh="getList"></updateDialog>
  </div>
</template>

<script>
import { listPost, delPost, exportPost } from "@/api/system/post";
import updateDialog from "./updateDialog.vue";
export default {
  name: "Post",
  components: {updateDialog},
  data() {
    return {
      loading: true, /**遮罩层*/ 
      ids: [],/**选中数组*/ 
      tableData: [], /**岗位表格数据*/ 
      dialogObj: {
        visible: false,
        id: '',
        title: '',
        width: '538px'
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      searchParams: { /**查询参数*/ 
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      tableDataObj: {
        id: 'id',
        showSelection: true,
        defaultExpandAll: true,
        treeProps: {children: 'children', hasChildren: 'hasChildren'},
        option: [
          {id: 1, label: '岗位编号', prop: 'postId', show: 1, needSlot: true},
          {id: 2, label: '岗位编码', prop: 'postCode', show: 1, needSlot: false},
          {id: 3, label: '岗位名称', prop: 'postName', show: 1, needSlot: false},
          {id: 4, label: '岗位排序', prop: 'postSort', align: 'center', show: 1, needSlot: false},
          {id: 5, label: '状态', prop: 'status', show: 1, needSlot: true},
          {id: 6, label: '创建时间', prop: 'createTime', show: 1, needSlot: false},
        ]
      },
    };
  },
  async created() {
    await this.getSysNormalDisable();
    this.getList();
  },
  methods: {
    async getList() {  /** 查询岗位列表 */
      this.loading = true;
      const data = {
        ...this.searchParams,
        ...this.queryParams
      }
      const res = await listPost(data);
      if(res.code == 200) {
        this.tableData = res.rows;
        this.queryParams.total = res.total;
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    },
    handleSearch() { /** 搜索按钮操作 */
      this.queryParams.pageNum = 1;
      this.queryParams.total = 0;
      this.tableData = [];
      this.getList();
    },
    handleReset() {  /** 重置按钮操作 */
      this.resetForm("searchParams", ()=>{
        this.handleSearch();
      });
    },
    handleSelectionChange(selection) { /**多选框选中数据 */
      this.ids = selection.map(item => item.postId);
    },
    handleAdd() { /** 新增按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.title = "添加岗位";
      this.dialogObj.id = "";
    },
    handleEdit(row) { /** 修改按钮操作 */
      this.dialogObj.visible = true;
      this.dialogObj.title = "修改岗位";
      this.dialogObj.id = row.postId;
    },
    handleDelete(row) {  /** 删除按钮操作 */
      const postIds = row.postId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(async ()=> {
        this.loading = true;
        const res = await delPost(postIds);
        if(res.code == 200) {
          this.handleSearch();
          this.$modal.msgSuccess("删除成功");
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      }).catch(() => {});
    },
    async handleExport() { /** 导出按钮操作 */
      this.loading = true;
      let res = await exportPost(this.queryParams);
      if(res.data && res.status == 200) {
        this.downloadFilesFn(res, `post_${this.formatDate()}.xlsx`);
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    }
  }
};
</script>
